//
//  SCCarImageUploadViewController.m
//  Shuaiche
//
//  Created by xiangtao on 2024/3/21.
//  Copyright © 2024 ShuaiChe. All rights reserved.
//

#import "SCCarImageUploadContainerVc.h"
#import "SCCarImageUploadVc.h"
#import "SCDynamicSegmentView.h"
#import "SCCertificateUploadVc.h"
#import "SCCarAddImageModel.h"
#import "MBProgressHUD+Add.h"
#import <Masonry/Masonry.h>

#define SegmentHeight 45
#define uploadBtnHeight 45

static NSString * const kTitle = @"title";
static NSString * const kDescription = @"description";

@interface SCCarImageUploadContainerVc ()<SCDynamicSegmentViewDelegate>
@property (nonatomic, strong) UIButton *uploadBtn;
@property (strong, nonatomic) SCDynamicSegmentView *segmengView;
@property (nonatomic, strong) SCCarImageUploadVc *carImageUploadVc;
@property (nonatomic, strong) SCCertificateUploadVc *certificateImageUploadVc;
@property (nonatomic, strong) SCCarImageUploadVc *otherImageUploadVc;
@property (nonatomic, strong) SCCarAddImageModel *carImageModel;

@end

@implementation SCCarImageUploadContainerVc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"上传图片";
    
//    [self prepareCarData];
    
    self.segmengView.titles = @[@"车辆图片",@"证件资料",@"其他照片"];
    [self.segmengView selectedAtIndex:self.imageUploadType];
        
    [self.view addSubview:self.uploadBtn];
    [self.uploadBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@15);
        make.right.equalTo(@-15);
        make.bottom.equalTo(@(kDevice_Is_iPhoneX ? -kSCScreenBottomOffset:-10));
        make.height.mas_equalTo(45);
    }];
    
    [self setupChild];
}

- (void)setupChild {
    CGFloat viewHeight = kSCScreenHeight-kSCNavigationHeight-SegmentHeight-uploadBtnHeight-(kDevice_Is_iPhoneX ? kSCScreenBottomOffset:10);
    
    _carImageUploadVc = [[SCCarImageUploadVc alloc] init];
    _carImageUploadVc.preShows = self.preShows;
    _carImageUploadVc.imageUploadType = ImageUploadTypeCar;
    _carImageUploadVc.maxPics = 16;
    _carImageUploadVc.notice = @"车辆图片在分享时会对外展示，长按可拖动可调整顺序。";
    _carImageUploadVc.frame = CGRectMake(0, SegmentHeight, kSCScreenWidth, viewHeight);
    [self.view addSubview:_carImageUploadVc.view];
    [self addChildViewController:_carImageUploadVc];
    
    _certificateImageUploadVc = [[SCCertificateUploadVc alloc] init];
    _certificateImageUploadVc.preShows = self.preShows;
    _certificateImageUploadVc.frame = CGRectMake(0, SegmentHeight, kSCScreenWidth, viewHeight);
    [self.view addSubview:_certificateImageUploadVc.view];
    [self addChildViewController:_certificateImageUploadVc];
    
    _otherImageUploadVc = [[SCCarImageUploadVc alloc] init];
    _otherImageUploadVc.preShows = self.preShows;
    _otherImageUploadVc.imageUploadType = ImageUploadTypeOther;
    _otherImageUploadVc.maxPics = 30;
    _otherImageUploadVc.notice = @"其他照片仅对公司内部人员展示。";
    _otherImageUploadVc.frame = CGRectMake(0, SegmentHeight, kSCScreenWidth, viewHeight);
    [self.view addSubview:_otherImageUploadVc.view];
    [self addChildViewController:_otherImageUploadVc];
    
    [self showUploadView];
}

- (void)showUploadView {
    if (self.imageUploadType == ImageUploadTypeCar) {
        [self.view bringSubviewToFront:_carImageUploadVc.view];
    } else if (self.imageUploadType == ImageUploadTypeCertificate) {
        [self.view bringSubviewToFront:_certificateImageUploadVc.view];
    } else if (self.imageUploadType == ImageUploadTypeOther) {
        [self.view bringSubviewToFront:_otherImageUploadVc.view];
    }
}


- (SCDynamicSegmentView *)segmengView{
    if(!_segmengView){
        SCDynamicSegmentView *segmentView = [[SCDynamicSegmentView alloc] initWithFrame:CGRectMake(0, 0, kSCScreenWidth, SegmentHeight)];
        segmentView.delegate = self;
        segmentView.hiddenBottomLine = YES;
        segmentView.clipsToBounds = YES;
        segmentView.itemWidth = (kSCScreenWidth-20*2)/3;
        _segmengView = segmentView;
        
        [self.view addSubview:segmentView];
    }
    return _segmengView;
}

#pragma mark- uploadBtn
- (UIButton *)uploadBtn {
    if (!_uploadBtn) {
        _uploadBtn = [[UIButton alloc] init];
        [_uploadBtn setTitle:@"确认上传" forState:0];
        [_uploadBtn setTitleColor:SC_TextBlack_Primary forState:0];
        _uploadBtn.backgroundColor = SC_Color_Tint;
        [_uploadBtn addTarget:self action:@selector(uploadButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _uploadBtn.layer.cornerRadius = 23;
        _uploadBtn.layer.masksToBounds = YES;
        [_uploadBtn.titleLabel setFont:SC_FONT(14)];
    }
    return _uploadBtn;
}

- (void)uploadButtonClick:(UIButton *)sender {
    NSArray *certArray = [_certificateImageUploadVc imagesArray];
    NSMutableArray *totalImages = [NSMutableArray arrayWithCapacity:0];
    [totalImages addObjectsFromArray:_carImageUploadVc.currentModel.imagesArray];
    [totalImages addObjectsFromArray:certArray];
    [totalImages addObjectsFromArray:_otherImageUploadVc.currentModel.imagesArray];
    NSMutableArray *marray = [NSMutableArray arrayWithCapacity:0];
    __block BOOL hasCarPic = NO;
    [totalImages enumerateObjectsUsingBlock:^(SCCarAddImageItemModel *model, NSUInteger idx, BOOL *stop){
        if (!model.isAddButton) {
            [marray addObject:model];
            if ([model.title isEqualToString:@"CAR_PIC"]) {
                hasCarPic = YES;
            }
        }
    }];
    
    if (!hasCarPic) {
        [MBProgressHUD showTip:@"请选择车辆图片" toView:nil];
        return;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showMessage:@"上传图片中,请稍等..." toView:nil];
    [SCImageUploadManager startUploadImages:marray completion:^(NSArray * _Nonnull picUrls,NSString *errorString) {
        NSLog(@"picUrls = %@",picUrls);
        if (picUrls) {
            int start = 0;
            [marray removeAllObjects];
            for (int i = 0; i < totalImages.count; i++) {
                SCCarAddImageItemModel *imageModel = totalImages[i];
                if (!imageModel.isAddButton) {
                    if (start > picUrls.count-1) {
                        break;
                    }
                    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
                    [dic setValue:picUrls[start] forKey:kPicUrl];
                    [dic setValue:imageModel.title forKey:kTitle];
                    [dic setValue:imageModel.carDescription forKey:kDescription];
                    [marray addObject:dic];
                    start++;
                }
            }
            if (self.completion) {
                self.completion(marray);
            }
            [hud switchShowSuccess:@"上传图片成功"];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [hud switchShowError:errorString];
        }
    }];
}

#pragma mark - 选项卡点击切换
- (void)segmentDidClickedAtIndex:(NSInteger)index{
    _imageUploadType = index;
    [self.segmengView selectedAtIndex:MAX(0, _imageUploadType)];
    [self showUploadView];
}

@end
